function init() {
	initPopup();
	
	initShowPopupTimer();
}

function initPopup() {
	var popupDiv = $("<div>", {
		id: "popup_window"
	})
		.addClass("popup-window")
		.css("display", "none");
	var formElem = $("<form>", {})
		.attr("method", "POST")
		.attr("target", "/cgi-bin/comment")
		.appendTo(popupDiv);
	addTextBox(formElem, "Ваше имя:", "input_name", false, true, "Введите, пожалуйста, имя!");
	addTextBox(formElem, "Ваш e-mail:", "input_email", false, false, null);
	addTextBox(formElem, "Адрес странички:", "input_page", false, false, null);
	addTextBox(formElem, "Комментарий:", "input_comment", true, true, "Введите, пожалуйста, комментарий!");
	
	var submitButton = $("<button>", {
		text: "Добавить!"
	})
		.addClass("yes-button")
		.attr("type", "button")
		.appendTo(formElem);
		
	var noButton = $("<button>", {
		text: "Не сейчас"
	})
		.addClass("no-button")
		.attr("type", "button")
		.appendTo(formElem);
	
	submitButton.click(function () {
		submitForm();
	});
	
	popupDiv.appendTo("body");
	
	try {
		parseCookies();
	}
	catch (err) {
		console.log(err);
	}
}

function parseCookies() {
	var cookies = document.cookie.split(";");
	for (var i = 0; i < cookies.length; i++) {
		var halves = cookies[i].trim().split("=");
		if (halves[0].trim() == "ZUI") {
			var c = unescape(halves[1].trim());
			var params = c.split("&");
			$("#input_name").val(params[0]);
			$("#input_email").val(params[1]);
			$("#input_page").val(params[2]);
			break;
		}
	}
}

function submitForm() {
	if (!validateForm()) {
		return;
	}
	var file = document.location.pathname;
	file = file.replace(/\/editors/g, "").replace(/.shtml/g, "").replace(/.html/g, "").replace(/.htm/g, "").replace(/\//g, "%2F");
	var postData = "FILE=" + file + "&MSGID=&OPERATION=store_new&NAME=" + encodeURIComponent($("#input_name").val()) + "&EMAIL=" + encodeURIComponent($("#input_email").val()) + "&URL=" + encodeURIComponent($("#input_page").val()) + "&TEXT=" + encodeURIComponent($("#input_comment").val()) + "&add=%C4%EE%E1%E0%E2%E8%F2%FC%21";
	
	showLoader();
	$.ajax({
		url: "/cgi-bin/comment",
		type: "POST",
		data: postData,
		contentType: "application/x-www-form-urlencoded",
		statusCode: {
			200: function (response) {
				//means we have some kind of an error, lol
				console.log("error");
			},
			302: function (response) {
				//means everything went smooth, double lol
				console.log("done");
			}
		},
		success: function() { hideLoader(); },
		error: function() { hideLoader(); }
	});
}

function showLoader() {
	$("<div>", {
		id: "loader_curtain"
	})
		.addClass("loader-curtain")
		.appendTo("#popup_window");
	$("<img>", {
		id: "loader_arrows"
	})
		.addClass("loader-arrows")
		.attr("src", "https://toothy-tooth.googlecode.com/svn/trunk/css/328.GIF")
		.appendTo("#popup_window");
}

function hideLoader() {
	$("#loader_arrows").remove();
	$("#loader_curtain").remove();
}

function validateForm() {
	var output = true;
	if (!$("#input_name").val()) {
		output = false;
		$("#input_name_error").css("display", "block");
	}
	else {
		$("#input_name_error").css("display", "none");
	}
	
	if (!$("#input_comment").val()) {
		output = false;
		$("#input_comment_error").css("display", "block");
	}
	else {
		$("#input_comment_error").css("display", "none");
	}
	return output;
}

function addTextBox(formElem, text, id, isLarge, isRequired, errorText) {
	var div = $("<div>", {})
		.addClass("popup-wrapper-div")
		.appendTo(formElem);
	$("<span>", {
		text: text
	}).appendTo(div);
	if (isRequired) {
		$("<span>", {
			text: "*"
		}).addClass("asterisk")
			.appendTo(div);
	}
	if (isLarge) {
		$("<textarea>", {
			id: id
		}).appendTo(div);
	}
	else {
		$("<input>", {
			id: id
		}).attr("type", "text")
			.appendTo(div);
	}
	if (isRequired) {
		$("<span>", {
			text: errorText,
			id: id + "_error"
		}).addClass("popup-error-text")
			.appendTo(div);
	}
}

function initShowPopupTimer() {
	setTimeout(function () {
		//TODO: animation
		$("#popup_window").css("display", "block");
		return "";
	}, 1000);
}

function waitForJQuery() {
    if (window.$){
        init();
    } else {
        setTimeout(waitForJQuery, 50);
    }
}

waitForJQuery();